/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.improvement.service.impl;

import com.koron.common.bean.StaffBean;
import com.koron.improvement.bean.AssessmentElement;
import com.koron.improvement.mapper.AssessmentElementMapper;
import com.koron.improvement.service.api.AssessmentStatisticService;
import com.koron.improvement.utils.BaseEntityUtil;
import com.koron.improvement.utils.BusinessCode;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.swan.bean.MessageBean;

import java.util.List;

@Service
public class AssessmentStatisticServiceImpl implements AssessmentStatisticService {

    private static final Logger logger = LoggerFactory.getLogger(AssessmentStatisticServiceImpl.class);

    MessageBean<?> checkList(List<AssessmentElement> assessmentElementList) {
        MessageBean<?> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), Object.class);
        for (AssessmentElement assessmentElement : assessmentElementList) {
            if (StringUtils.isBlank(assessmentElement.getElementName())) {
                msg.setCode(BusinessCode.EMPTY_ELEMENT_NAME.getCode());
                msg.setDescription(BusinessCode.EMPTY_ELEMENT_NAME.getDescription());
                return msg;
            }
            if (assessmentElement.getAssessmentType() == null) {
                msg.setCode(BusinessCode.EMPTY_ELEMENT_TYPE.getCode());
                msg.setDescription(BusinessCode.EMPTY_ELEMENT_TYPE.getDescription());
                return msg;
            }
            if (assessmentElement.getBenchmarkScore() == null) {
                msg.setCode(BusinessCode.EMPTY_ELEMENT_BENCHMARK_SCORE.getCode());
                msg.setDescription(BusinessCode.EMPTY_ELEMENT_BENCHMARK_SCORE.getDescription());
                return msg;
            }
        }
        return msg;
    }

    @Override
    public MessageBean<?> saveAssessmentElement(StaffBean user, List<AssessmentElement> assessmentElementList) {
        MessageBean<?> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), Object.class);
        if (assessmentElementList == null || assessmentElementList.isEmpty()) {
            msg.setCode(20900);
            msg.setDescription("保存内容不能为空");
            return msg;
        }
        SessionFactory factory = new SessionFactory();
        try {
            msg = checkList(assessmentElementList);
            if (msg.getCode() != 0) {
                return msg;
            }
            for (AssessmentElement assessmentElement : assessmentElementList) {
                AssessmentElementMapper elementMapper = factory.getMapper(AssessmentElementMapper.class);
                AssessmentElement cond = new AssessmentElement();
                cond.setAssessmentType(assessmentElement.getAssessmentType());
                cond.setElementName(assessmentElement.getElementName());
                List<AssessmentElement> list = elementMapper.selectByCond(cond);
                if (list != null && !list.isEmpty()) {
                    if (StringUtils.isNotBlank(assessmentElement.getId())) {
                        boolean dup = list.stream().anyMatch(element -> !element.getId().equals(assessmentElement.getId()));
                        if (dup) {
                            msg.setCode(BusinessCode.DUPLICATE_ELEMENT_NAME.getCode());
                            msg.setDescription(BusinessCode.DUPLICATE_ELEMENT_NAME.getDescription());
                            factory.close(false);
                            return msg;
                        }
                    } else {
                        msg.setCode(BusinessCode.DUPLICATE_ELEMENT_NAME.getCode());
                        msg.setDescription(BusinessCode.DUPLICATE_ELEMENT_NAME.getDescription());
                        factory.close(false);
                        return msg;
                    }
                }
                if (StringUtils.isBlank(assessmentElement.getId())) {
                    BaseEntityUtil.setAddBaseProperties(assessmentElement, user);
                    elementMapper.insert(assessmentElement);
                } else {
                    BaseEntityUtil.setUpdateBaseProperties(assessmentElement, user);
                    elementMapper.updateByPrimaryKeySelective(assessmentElement);
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription(BusinessCode.FAILURE.getDescription());
            factory.close(false);
            return msg;
        } finally {
            if (factory != null) {
                factory.close();
            }
        }
        return msg;
    }

    @Override
    public MessageBean<?> getAssessmentElementList(StaffBean user, Integer assessmentType) {
        MessageBean<List> msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), List.class);
        if (assessmentType == null) {
            msg.setCode(BusinessCode.EMPTY_ELEMENT_TYPE.getCode());
            msg.setDescription(BusinessCode.EMPTY_ELEMENT_TYPE.getDescription());
            return msg;
        }
        try (SessionFactory factory = new SessionFactory()) {
            AssessmentElementMapper elementMapper = factory.getMapper(AssessmentElementMapper.class);
            AssessmentElement cond = new AssessmentElement();
            cond.setAssessmentType(assessmentType);
            List<AssessmentElement> list = elementMapper.selectByCond(cond);
            msg.setData(list);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            msg.setCode(BusinessCode.FAILURE.getCode());
            msg.setDescription(BusinessCode.FAILURE.getDescription());
        }
        return msg;
    }
}
